// pages/mission_check_info/index.js
var router = require('../../utils/router/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: false,
    hospitaltext: '选择医院',
    hospital_id:'', // 意向医院id
    donate_id: '' // 捐赠单id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const data = router.extract(options);
    this.setData({
      donate_id:data.id,
      donate_name:data.donate_name
      }
    )
    
    //首页数据
    this.get_hospital_list()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 我清楚啦
   */
  bind_next_step: function(){
    var that = this
    if(that.data.donate_id == '' || that.data.hospital_id == '' ||that.data.hospital_name  == '' ||that.data.donate_name == ''){
      console.log('信息不能为空')
      return
    }
    router.push({ 
      name: 'add_donationlist',
      data: {
        donate_id: that.data.donate_id,
        donate_name:that.data.donate_name,
        hospital_id: that.data.hospital_id,
        hospital_name:that.data.hospital_name
        } 
      })
  },

  // bindShowMsg() {
  //   this.setData({
  //       select:!this.data.select
  //   })
  // },
  
  // mySelect(e) {
  //   var name = e.currentTarget.dataset.name
  //   this.setData({
  //       tihuoWay: name,
  //       select: false
  //   })
  // },

   /**
   * 主页医院数据
   */
  get_hospital_list(){
    var that = this
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      // 云函数名称
      name: 'hospital',
      // 传给云函数的参数
      data:{
        "action": "get_hospital_list",
        "offset": 0,
        "limit": 10,
        "filter": {
         "hospital_name": {
            "$regex":".*协和",
            "$options": "i"
          }
        }
      }
    })
    .then(res => {
      console.log(res.result) // 3
      var hospitalList = res.result.data;
      var hospitalLists = new Array();
      for (var i = 0; i < hospitalList.length; i++) {
        console.log(hospitalList[i].hospital_name);
        hospitalLists.push(hospitalList[i].hospital_name);
      }
      that.setData({
        hospitaltextLists: hospitalLists,
        hospitalList: res.result.data,
      });

    wx.hideNavigationBarLoading();
    wx.hideLoading()
    })
    .catch(console.error);
    // wx.hideNavigationBarLoading();
    // wx.hideLoading()
  },

  /**
   * 选择医院
   */
  bindhospitalChange: function(e){
    var that = this
    this.setData({
      index: e.detail.value,
      hospitaltext:'',
      hospital_need: that.data.hospitalList[e.detail.value].hospital_need,
      hospital_id:that.data.hospitalList[e.detail.value]._id,
      hospital_name:that.data.hospitalList[e.detail.value].hospital_name
    })
  }

})